<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="授权应用信息" @del="del" @newCreate="newCreate"></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" :api="apiMap.getListApi" :columnSource="baseColumns" :cbFun="cbFun" @selection-change="data => tableSelectData = data"
            @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'授权应用信息'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" labelWidth="120px" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle'

export default {
  name: 'authApp',
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        id: null,
        name: '',
        type:'',
        appkey: '',
        appsecret: '',
        getway: '',
        authurl: '',
      },

    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          invalid: 'newCreate'
        },
        {
          label: this.$t('common.delete'),
          disabled: true,
          invalid: 'del'
        }
      ]
    },
    baseColumns() {
      return [
        {
          label: '主键ID',
          prop: 'id',
          input: 'input'
        },
        {
          label: '应用名称',
          prop: 'name',
          input: 'input'
        },
        {
          label: '应用所属平台',
          prop: 'type',
          input: 'select',
          options: [
            { label: '淘宝', value: 'TB' },
            { label: '抖音', value: 'DY' },
            { label: '拼多多', value: 'PDD' },
            { label: '京东', value: 'JD' },
            { label: '小红书', value: 'XHS' },
            { label: '得物', value: 'DW' },
            { label: '唯品', value: 'WP' },
          ]

        },
        {
          label: '应用id',
          prop: 'appkey',
          input: 'input',
        },
        {
          label: '应用密钥',
          prop: 'appsecret',
          input: 'input',
        },
        {
          label: '网关地址',
          prop: 'getway',
          input: 'input'
        },
        {
          label: '授权地址',
          prop: 'authurl',
          input: 'input'
        },
        {
          label: '回调地址',
          prop: 'redirecturl',
          input: 'input'
        },
      ]
    },
    formConfig() {
      return [
        {
          label: '应用名称',
          prop: 'name',
          type: 'input'
        },
        {
          label: '应用所属平台',
          prop: 'type',
          type: 'select',
          options: [
            { label: '淘宝', value: 'TB' },
            { label: '抖音', value: 'DY' },
            { label: '拼多多', value: 'PDD' },
            { label: '京东', value: 'JD' },
            { label: '小红书', value: 'XHS' },
            { label: '得物', value: 'DW' },
            { label: '唯品', value: 'WP' },
          ]
        },
        {
          label: '应用id',
          prop: 'appkey',
          type: 'input',
          tip:'appkey',
        },
        {
          label: '应用密钥',
          prop: 'appsecret',
          type: 'input',
          tip:'appsecret',
        },
        {
          label: '网关地址',
          prop: 'getway',
          type: 'input'
        },
        {
          label: '授权地址',
          prop: 'authurl',
          type: 'input'
        },
        {
          label: '回调地址',
          prop: 'redirecturl',
          type: 'input'
        },

      ]
    },
    formRules() {
      return {
        name: [
          {
            required: true,
            message: '请输入应用名称',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请选择平台类型',
            trigger: 'change'
          }
        ],
        appkey: [
          {
            required: true,
            message: '请输入应用id',
            trigger: 'blur'
          }
        ],
        appSecret: [
          {
            required: true,
            message: '请输入应用密钥',
            trigger: 'blur'
          }
        ],
        authurl: [
          {
            required: true,
            message: '请输入授权地址',
            trigger: 'blur'
          }
        ],

      }
    },
    apiMap() {
      return {
        getListApi: '/v1/plAuthApp/qryPlAuthAppList',
        getInfoApi:'/v1/plAuthApp/qryPlAuthAppById',
        editApi:'/v1/plAuthApp/updPlAuthApp',
        deleteApi: '/v1/plAuthApp/delPlAuthApps',
        addApi: '/v1/plAuthApp/addPlAuthApp'
      }
    }
  },
  methods: {
    handleConfirm() {
      const params = this._.cloneDeep(this.form)

      let promise
      if (this.dialogModule.type == 'edit') {
        promise = this.$post(this.apiMap.editApi, params)
      } else {
        promise = this.$post(this.apiMap.addApi, params)
      }
      promise.then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
        this.$refs.detail?.resetFields()
        this.dialogModule.flag = false
      })
    },
    getInfoById(id) {
      this.$post(this.apiMap.getInfoApi, {
        id
      }).then(res => {
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.name}]授权应用信息`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增授权应用信息'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$confirm('是否删除所选项?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
        this.$post(this.apiMap.deleteApi, data).then(res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
        })
      }).catch(() => {
      });
    },

    closed() {
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
  },
  mounted() {

  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
